﻿Imports System.ComponentModel
Imports System.Data
Imports Global_Visas_Business_Layer
Imports System.Text

Partial Public Class frmAdminProductTypes
    Private intSaveNeworEdit As Integer
    Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAddNew.Click
        intSaveNeworEdit = 1
        ClearControls()
        EnableDisableDataEntryControls(True)
        Me.btnAddNew.IsEnabled = True
        Me.btnSave.IsEnabled = True
        Me.btnEdit.IsEnabled = True
        Me.btnCancel.IsEnabled = True
        Me.btnDelete.IsEnabled = True
        Me.txtProductName.Focus()
    End Sub

    Private Sub EnableDisableDataEntryControls(ByVal enableFlag As Boolean)
        Me.txtProductName.IsEnabled = enableFlag
        Me.drpBranch.IsEnabled = enableFlag
        Me.drpChargeGroup.IsEnabled = enableFlag
        Me.drpEventGroup.IsEnabled = enableFlag
        Me.drpMFBGroup.IsEnabled = enableFlag
        Me.drpResidence.IsEnabled = enableFlag
        Me.drpSLS.IsEnabled = enableFlag
        Me.drpStageDiagram.IsEnabled = enableFlag
        Me.drpFastTrack.IsEnabled = enableFlag
        Me.drpDestination.IsEnabled = enableFlag
        Me.drpDepartment.IsEnabled = enableFlag
    End Sub


    Private Sub ClearControls()
        Me.txtProductName.Text = String.Empty
        Me.drpBranch.SelectedIndex = -1
        Me.drpChargeGroup.SelectedIndex = -1
        Me.drpEventGroup.SelectedIndex = -1
        Me.drpMFBGroup.SelectedIndex = -1
        Me.drpResidence.SelectedIndex = -1
        Me.drpSLS.SelectedIndex = -1
        Me.drpStageDiagram.SelectedIndex = -1
        Me.drpFastTrack.SelectedIndex = -1
        Me.drpDestination.SelectedIndex = -1
        Me.drpDepartment.SelectedIndex = -1
    End Sub

    Private Sub frmAdminProductTypes_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        FillAllDetails()
        refreshList()
        EnableDisableDataEntryControls(False)
        Me.drpBranch.IsEnabled = False
        Me.btnSave.IsEnabled = False
        Me.btnEdit.IsEnabled = False
        Me.btnCancel.IsEnabled = False
        Me.btnDelete.IsEnabled = False
    End Sub

    Private Sub FillAllDetails()
        With Me.drpBranch
            .SelectedValuePath = "Branch_ID"
            .DisplayMemberPath = "Branch_Name"
            .ItemsSource = CType(CommonDataFunctions.BranchList, IListSource).GetList
        End With

        With Me.drpStageDiagram
            .SelectedValuePath = "StageDiagram_ID"
            .DisplayMemberPath = "StageDiagram_Name"
            .ItemsSource = CType(Products.StageDiagramList, IListSource).GetList
        End With

        With Me.drpChargeGroup
            .SelectedValuePath = "ChargeGroup_ID"
            .DisplayMemberPath = "Charge Groups"
            .ItemsSource = CType(Admin.ChargeGroupsList, IListSource).GetList
        End With
        With Me.drpDepartment
            .SelectedValuePath = "Department_ID"
            .DisplayMemberPath = "Department_Name"
            .ItemsSource = CType(CommonDataFunctions.DepartmentList, IListSource).GetList
        End With
        With Me.drpDestination
            .SelectedValuePath = "Country_ID"
            .DisplayMemberPath = "Country"
            .ItemsSource = CType(Admin.CountryList, IListSource).GetList
        End With

        With Me.drpResidence
            .SelectedValuePath = "Country_ID"
            .DisplayMemberPath = "Country"
            .ItemsSource = CType(Admin.CountryList, IListSource).GetList
        End With

        With Me.drpEventGroup
            .SelectedValuePath = "EventGroup_ID"
            .DisplayMemberPath = "Event Groups"
            .ItemsSource = CType(Admin.EventGroupsList, IListSource).GetList
        End With
    End Sub

    Private Sub refreshList()
        Dim productTypeList As DataTable = Products.ProductTypesList()
        If productTypeList.Rows.Count <> 0 Then
            Me.grdProductTypes.IsEnabled = True
            Me.grdProductTypes.ItemsSource = CType(productTypeList, IListSource).GetList
            Me.grdProductTypes.Columns(0).IsVisible = False
            Me.grdProductTypes.Columns(2).IsVisible = False
            Me.grdProductTypes.Columns(3).IsVisible = False
            Me.grdProductTypes.Columns(4).IsVisible = False
            Me.grdProductTypes.Columns(5).IsVisible = False
            Me.grdProductTypes.Columns(6).IsVisible = False
            Me.grdProductTypes.Columns(7).IsVisible = False
            Me.grdProductTypes.Columns(8).IsVisible = False
            Me.grdProductTypes.Columns(9).IsVisible = False
            Me.grdProductTypes.Columns(10).IsVisible = False
            Me.grdProductTypes.Columns(11).IsVisible = False
            Me.grdProductTypes.Columns(12).IsVisible = False
            Me.grdProductTypes.Columns(13).IsVisible = False
            Me.grdProductTypes.Columns(14).IsVisible = False
            Me.grdProductTypes.Columns(15).IsVisible = False
            Me.grdProductTypes.Columns(16).IsVisible = False
        Else
            Me.grdProductTypes.IsEnabled = False
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSave.Click
        If txtProductName.Text.Trim = String.Empty Then
            MsgBox("Please enter Product Type Name !")
            txtProductName.Focus()
            Exit Sub
        ElseIf drpDestination.SelectedIndex = -1 Then
            MsgBox("Please select Destination !")
            Exit Sub
        ElseIf drpDepartment.SelectedIndex = -1 Then
            MsgBox("Please select Department!")
            Exit Sub
        ElseIf drpStageDiagram.SelectedIndex = -1 Then
            MsgBox("Please select StageDiagram !")
            Exit Sub
        ElseIf drpChargeGroup.SelectedIndex = -1 Then
            MsgBox("Please select Charge Group !")
            Exit Sub
        ElseIf drpEventGroup.SelectedIndex = -1 Then
            MsgBox("Please select Event Group !")
            Exit Sub
        ElseIf drpBranch.SelectedIndex = -1 Then
            MsgBox("Please select Branch!")
            Exit Sub
        Else
            btnSave.IsEnabled = False
            Dim sql As StringBuilder
            Dim destinationId As Integer = drpDestination.SelectedValue
            Dim departmentId As Integer = drpDepartment.SelectedValue
            Dim stageDiagramId As Integer = drpStageDiagram.SelectedValue
            Dim chargeGroupId As Integer = drpChargeGroup.SelectedValue
            Dim eventGroupId As Integer = drpEventGroup.SelectedValue
            Dim branchId As Integer = drpBranch.SelectedValue
            Dim residenceId As Integer = drpResidence.SelectedValue
            Select Case intSaveNeworEdit
                Case 1 'new one
                    sql = New StringBuilder
                    With sql
                        .Append("")
                        .Append("insert into [GLOBAL].[dbo].[L_ProductTypes] ")
                        .Append("([ProductType_Name],[ProductType_DestinationID] ,[ProductType_DepartmentID],")
                        .Append("[ProductType_StageDiagramID],[ProductType_MFBGroupID],[ProductType_SLSID],[ProductType_ChargeGroupID],[ProductType_EventGroupID],")
                        .Append("[ProductType_WebLoopID],[ProductType_FastTrack],[ProductType_DTCategory],[ProductType_CalcSet],[ProductType_TargetGroupID],")
                        .Append("[ProductType_BranchID],[PRODUCTTYPE_DTDISABLED],[ProductType_ResidenceID] )")
                        .Append(" Values('" & txtProductName.Text & "'," & destinationId & "," & departmentId & ",")
                        .Append(stageDiagramId & ",1,1," & chargeGroupId & "," & eventGroupId & ",")
                        .Append("1,1,1,1,1,")
                        .Append(branchId & ",''," & residenceId & ")")
                    End With

                    If CommonDataFunctions.ExecuteSQL(sql.ToString) = True Then
                        refreshList()
                        MsgBox("The new Product type has been added successfully.", MsgBoxStyle.Information, "Success")
                        'Exit Sub
                    Else
                        MsgBox("There is an error adding the Department to the system. Please try again or contact IT", MsgBoxStyle.Critical, "Unsuccessful")
                        'Exit Sub
                    End If
                Case 2 'existing one
                    sql = New StringBuilder
                    With sql
                        .Append("update [GLOBAL].[dbo].[L_ProductTypes]  set ")
                        .Append(" [ProductType_Name] = '" & Me.txtProductName.Text.Trim() & "',")
                        .Append(" [ProductType_DestinationID] =" & destinationId & ",")
                        .Append(" [ProductType_DepartmentID] =" & departmentId & ",")
                        .Append(" [ProductType_StageDiagramID] =" & stageDiagramId & ",")
                        .Append(" [ProductType_MFBGroupID] =" & 1 & ",")
                        .Append(" [ProductType_SLSID] =" & 1 & ",")
                        .Append(" [ProductType_ChargeGroupID] =" & chargeGroupId & ",")
                        .Append(" [ProductType_EventGroupID] =" & eventGroupId & ",")
                        .Append(" [ProductType_WebLoopID] =" & 1 & ",")
                        .Append(" [ProductType_FastTrack] =" & 1 & ",")
                        .Append(" [ProductType_DTCategory] =" & 1 & ",")
                        .Append(" [ProductType_CalcSet] =" & 1 & ",")
                        .Append(" [ProductType_TargetGroupID] =" & 1 & ",")
                        .Append(" [ProductType_BranchID] =" & branchId & ",")
                        .Append(" [PRODUCTTYPE_DTDISABLED] =" & "''" & ",")
                        .Append(" [ProductType_ResidenceID] =" & residenceId)
                        Dim selectedRow As DataRowView = grdProductTypes.SelectedItem
                        .Append(" where [ProductType_ID] = " & selectedRow(0))
                    End With

                    If CommonDataFunctions.ExecuteSQL(sql.ToString) = True Then
                        refreshList()
                        MsgBox("The Product Type's details have been updated successfully.", MsgBoxStyle.Information, "Success")
                        'Exit Sub
                    Else
                        MsgBox("There is an error in editing the Product Type's details. Please try again or contact IT", MsgBoxStyle.Critical, "Unsuccessful")
                        'Exit Sub
                    End If

            End Select
            ClearControls()
            EnableDisableDataEntryControls(False)
            grdProductTypes.IsEnabled = True
            btnCancel.IsEnabled = False
            btnAddNew.IsEnabled = True
            btnEdit.IsEnabled = False
            btnDelete.IsEnabled = False
        End If

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCancel.Click
        intSaveNeworEdit = 0
        ClearControls()
        EnableDisableDataEntryControls(False)
        btnSave.IsEnabled = False
        btnEdit.IsEnabled = False
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnEdit.Click
        Dim selectedRow As DataRowView = grdProductTypes.SelectedItem
        If IsNothing(selectedRow) Then
            Exit Sub
        End If
        EnableDisableDataEntryControls(True)
        btnSave.IsEnabled = True
        btnCancel.IsEnabled = True
        intSaveNeworEdit = 2
    End Sub

    Private Sub grdProductTypes_SelectionChanged(ByVal sender As System.Object, ByVal e As Telerik.Windows.Controls.SelectionChangeEventArgs) Handles grdProductTypes.SelectionChanged
        Dim selectedRow As DataRowView = grdProductTypes.SelectedItem
        If Not IsNothing(selectedRow) Then
            txtProductName.Text = selectedRow("Product Type").ToString()
            drpDestination.SelectedValue = Convert.ToInt32(selectedRow("ProductType_DestinationID").ToString())
            drpDepartment.SelectedValue = Convert.ToInt32(selectedRow("ProductType_DepartmentID").ToString())
            drpStageDiagram.SelectedValue = Convert.ToInt32(selectedRow("ProductType_StageDiagramID").ToString())
            drpMFBGroup.SelectedValue = Convert.ToInt32(selectedRow("ProductType_MFBGroupID").ToString())
            drpSLS.SelectedValue = Convert.ToInt32(selectedRow("ProductType_SLSID").ToString())
            drpChargeGroup.SelectedValue = Convert.ToInt32(selectedRow("ProductType_ChargeGroupID").ToString())
            drpEventGroup.SelectedValue = Convert.ToInt32(selectedRow("ProductType_EventGroupID").ToString())
            ',[ProductType_WebLoopID]
            drpFastTrack.SelectedValue = Convert.ToInt32(selectedRow("ProductType_FastTrack").ToString())
            ',[ProductType_DTCategory]
            ',[ProductType_CalcSet]
            ',[ProductType_TargetGroupID]
            drpBranch.SelectedValue = Convert.ToInt32(selectedRow("ProductType_BranchID").ToString())
            ',[PRODUCTTYPE_DTDISABLED]
            drpResidence.SelectedValue = Convert.ToInt32(selectedRow("ProductType_ResidenceID").ToString())
            btnEdit.IsEnabled = True
        Else
            Exit Sub
        End If
    End Sub
End Class
